﻿/*****************************************************************
// Copyright (C)
// All rights reserved.
// 
// Author:       WYQ
// Create Date:  2012-8-18 15:16:04
// Usage: 持久化数据库仓库
*****************************************************************/
using System;
using System.Linq;
using System.Linq.Expressions;

namespace Portal.Ef.Repository
{

   
    public class  UserRepository : Repository<User, User, TbUser>, IUserRepository<User>
    {
        public override User CreateEntity()
        {
            return new User { };
        }
        public override IQueryable<User> Get()
        {
            return MapQueryable(Entities.TbUser);
        }
        public override long Count()
        {
            return MapQueryable(Entities.TbUser).Count();
        }
        public override IQueryable<User> Get(Expression<Func<User, bool>> expression)
        {
            return MapQueryable(Entities.TbUser).Where(expression);
        }
        public override IQueryable<User> GetSort(string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbUser), sort, order);
        }
        public override IQueryable<User> GetSort(Expression<Func<User, bool>> expression, string sort = "Id", string order = "DESC")
        {
            Check.Argument.IsNotNull(expression, "查询条件");
            Check.Argument.IsNotNull(sort, "排序字段");
            Check.Argument.IsNotNull(order, "排序条件");
            return LinqHelper.DataSorting(MapQueryable(Entities.TbUser).Where(expression), sort, order);
        }
        public override void Add(User t)
        {
            Entities.AddToTbUser(ReverseMapModel(t));
        }
        public override void Update(User t)
        {
            throw new NotImplementedException();
        }
        public override void AddOrUpdate(User t)
        {
            if (Entities.TbUser.Any(o => o.CoUserId == t.Id))
            {

            }
            else
            {
                Entities.AddToTbUser(ReverseMapModel(t));
            }
        }
        public override void Delete(User t)
        {
            Entities.TbUser.Where(o => o.CoUserId == t.Id).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Delete(Expression<Func<User, bool>> expression)
        {
            MapQueryable(Entities.TbUser).Where(expression).ForEach(o => Entities.DeleteObject(o));
        }
        public override void Save()
        {
            Entities.SaveChanges();
        }
       
        public JqEasyuiDataGrid<User> JqEasyuiDataGrid(string code, string name, string categoryId, string sort = "CreateTime", string order = "Desc", int page = 10, int index = 1)
        {
            return new JqEasyuiDataGrid<User>();
        }
      
        public override sealed IQueryable<User> MapQueryable(IQueryable<TbUser> queryable)
        {
            return queryable.Select(o => new User 
            {
         
	           Id=o.CoUserId,
	           Name = o.CoName 
,
	           CreateTime = o.CoCreateTime??DateTime.Now 
,
	           IsDelete = o.CoIsDelete??false 
,
	           Version = o.CoVersion??0 
,
	           IsClosed = o.CoIsClosed??false 
,
	           Culture = o.CoCulture 
,
	           Account = o.CoAccount 
,
	           Password = o.CoPassword 
,
	           Code = o.CoCode 
,
	           RoleId = o.CoRoleId 
,
	           CategoryId = o.CoCategoryId 
,
	           LastAccessTime = o.CoLastAccessTime??DateTime.Now 
,
	           Level = o.CoLevel??0 
,
	           LevelFlagImage = o.CoLevelFlagImage 
            
            });
        }
      
        public override sealed IQueryable<TbUser> ReverseMapQueryable(IQueryable<User> queryable)
        {
            return queryable.Select(o => new TbUser 
            { 
        
	           CoUserId=o.Id,
	           CoName = o.Name
,
	           CoCreateTime = o.CreateTime
,
	           CoIsDelete = o.IsDelete
,
	           CoVersion = o.Version
,
	           CoIsClosed = o.IsClosed
,
	           CoCulture = o.Culture
,
	           CoAccount = o.Account
,
	           CoPassword = o.Password
,
	           CoCode = o.Code
,
	           CoRoleId = o.RoleId
,
	           CoCategoryId = o.CategoryId
,
	           CoLastAccessTime = o.LastAccessTime
,
	           CoLevel = o.Level
,
	           CoLevelFlagImage = o.LevelFlagImage
  
            
            });
        }
      
        public override sealed User MapModel(TbUser model)
        {
            return new User {
	           Id=model.CoUserId,
	           Name = model.CoName
,
	           CreateTime = model.CoCreateTime??DateTime.Now
,
	           IsDelete = model.CoIsDelete??false
,
	           Version = model.CoVersion??0
,
	           IsClosed = model.CoIsClosed??false
,
	           Culture = model.CoCulture
,
	           Account = model.CoAccount
,
	           Password = model.CoPassword
,
	           Code = model.CoCode
,
	           RoleId = model.CoRoleId
,
	           CategoryId = model.CoCategoryId
,
	           LastAccessTime = model.CoLastAccessTime??DateTime.Now
,
	           Level = model.CoLevel??0
,
	           LevelFlagImage = model.CoLevelFlagImage
            };
        }
        
        public override sealed TbUser ReverseMapModel(User model)
        {
            return new TbUser { 
	           CoUserId=model.Id,
	           CoName = model.Name
	        
,
	           CoCreateTime = model.CreateTime
	        
,
	           CoIsDelete = model.IsDelete
	        
,
	           CoVersion = model.Version
	        
,
	           CoIsClosed = model.IsClosed
	        
,
	           CoCulture = model.Culture
	        
,
	           CoAccount = model.Account
	        
,
	           CoPassword = model.Password
	        
,
	           CoCode = model.Code
	        
,
	           CoRoleId = model.RoleId
	        
,
	           CoCategoryId = model.CategoryId
	        
,
	           CoLastAccessTime = model.LastAccessTime
	        
,
	           CoLevel = model.Level
	        
,
	           CoLevelFlagImage = model.LevelFlagImage
	        
            
            
            };
        }


    }
}



